package com.djhu.freepartner.web.control;


import com.baomidou.mybatisplus.plugins.Page;
import com.djhu.freepartner.bean.PartnerConsumeRecord;
import com.djhu.freepartner.bean.PartnerUser;
import com.djhu.freepartner.core.RestRsp;
import com.djhu.freepartner.core.Status;
import com.djhu.freepartner.core.exception.ServiceException;
import com.djhu.freepartner.service.IPartnerConsumeRecordService;
import com.djhu.freepartner.service.IPartnerUserService;
import com.djhu.freepartner.service.model.BonuspointSearchModel;
import com.djhu.freepartner.web.control.model.Bonuspoint;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController("/points")
@Api("积分相关api")
public class BonuspointsControl {

    @Autowired
    private IPartnerUserService partnerUserService;

    @Autowired
    private IPartnerConsumeRecordService  partnerConsumeRecordService;


    @ApiOperation(value="查看用户消费积分", notes="查看用户积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "BonuspointSearchModel", value = "查看用户消费记录", required = true, dataType = "BonuspointSearchModel")
    })
    @RequestMapping(value = "/pointlog",method = RequestMethod.POST)
    public RestRsp pointRecord(@RequestBody BonuspointSearchModel searchModel) throws ServiceException {
        String userid = searchModel.getUserid();
        if (StringUtils.isEmpty(userid)){
            throw new ServiceException(Status.ACCOUNT_NOT_FOUND);
        }
        PartnerUser user = partnerUserService.selectById(userid);
        if (user == null){
            throw new ServiceException(Status.ACCOUNT_NOT_FOUND);
        }
        Page<PartnerConsumeRecord> consumeRecords = partnerConsumeRecordService.selectConsumerLogByPage(searchModel);
        RestRsp restRsp = new RestRsp();
        restRsp.setCode(Status.SUCCESS.getCode());
        restRsp.setData(consumeRecords);
        return restRsp;
    }

    @ApiOperation(value="查看用户剩余积分", notes="查看用户剩余积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonObject", value = "用户的ID", required = true, dataType = "JSONObject")
    })
    @PostMapping("/userPoint")
    public RestRsp userPoint(@RequestBody  JSONObject jsonObject) throws ServiceException {
        String userid = jsonObject.getString("userid");
        if (StringUtils.isEmpty(userid)){
            throw new ServiceException(Status.ACCOUNT_NOT_FOUND);
        }
        PartnerUser user = partnerUserService.selectById(userid);
        if (user == null){
            throw new ServiceException(Status.ACCOUNT_NOT_FOUND);
        }
        RestRsp restRsp = new RestRsp();
        restRsp.setCode(Status.SUCCESS.getCode());
        Map<String,Object> objectMap = new HashMap<>();
        objectMap.put("userid",user.getUserid());
        objectMap.put("userpoints",user.getAccount());
        restRsp.setData(objectMap);
        return restRsp;
    }

    @ApiOperation(value="用户积分充值接口", notes="用户充值接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户的ID", required = true, dataType = "Bonuspoint"),
            @ApiImplicitParam(name = "account", value = "充值金额", required = true, dataType = "int")
    })
    @PostMapping("/userPointAdd")
    public RestRsp userPointAdd(String userid,String account){

        return null;
    }


    @ApiOperation(value="用户消费积分接口", notes="用户消费积分接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户的ID", required = true, dataType = "Bonuspoint"),
    })
    @PostMapping("/userPointConsume")
    public RestRsp userPointConsume(@RequestBody Bonuspoint bonuspoint){
        return null;
    }






}
